package com.getpebble.android.common.model;

import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class PblDataModel {
    private final Map<Column, Column> mColumnMap = new HashMap();
    private String mTableName = null;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$common$model$PblDataModel$Column$Type = new int[Type.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$common$model$PblDataModel$Column$Type[Type.INTEGER.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PblDataModel$Column$Type[Type.STRING.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$model$PblDataModel$Column$Type[Type.TIMESTAMP.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public static class Column {
        private String mDefaultValue = null;
        private String mName = null;
        private Type mType = null;
        private boolean mUnique = false;

        public enum Type {
            INTEGER,
            STRING,
            TIMESTAMP
        }

        public Column(Type type, String name) throws IllegalArgumentException {
            if (type == null || name == null) {
                throw new IllegalArgumentException("'type' and 'name' must not be null!");
            }
            this.mType = type;
            this.mName = name;
        }

        public String getName() {
            return this.mName;
        }

        public boolean isUnique() {
            return this.mUnique;
        }

        public void setUnique(boolean unique) {
            this.mUnique = unique;
        }

        public void setDefault(String defaultValue) {
            this.mDefaultValue = defaultValue;
        }

        public String getDefaultValue() {
            return this.mDefaultValue;
        }

        public String getSqlType() throws IllegalStateException {
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$common$model$PblDataModel$Column$Type[this.mType.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    return "INTEGER";
                case SMTPMessage.RETURN_HDRS /*2*/:
                    return "VARCHAR";
                case ListInfo.INDETERMINATE /*3*/:
                    return "TIMESTAMP";
                default:
                    throw new IllegalStateException("Invalid type!");
            }
        }
    }

    public PblDataModel(String tableName) throws IllegalArgumentException {
        if (tableName == null) {
            throw new IllegalArgumentException("'tableName' cannot be null!");
        }
        this.mTableName = tableName;
        addColumn(new Column(Type.INTEGER, "_id"));
        addColumn(new Column(Type.INTEGER, "_is_dirty"));
        Column dateCreated = new Column(Type.TIMESTAMP, "_date_created");
        dateCreated.setDefault("CURRENT_TIMESTAMP");
        addColumn(dateCreated);
        Column dateUpdated = new Column(Type.TIMESTAMP, "_date_updated");
        dateUpdated.setDefault("CURRENT_TIMESTAMP");
        addColumn(dateUpdated);
        Column needsAdd = new Column(Type.INTEGER, "_needs_add");
        needsAdd.setDefault("0");
        addColumn(needsAdd);
        Column needsDelete = new Column(Type.INTEGER, "_needs_delete");
        needsDelete.setDefault("0");
        addColumn(needsDelete);
    }

    public String getTableName() {
        return this.mTableName;
    }

    public String getCreateTableCommand() {
        StringBuilder createTableQuery = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
        createTableQuery.append(getTableName());
        createTableQuery.append(" (");
        for (Column column : this.mColumnMap.keySet()) {
            createTableQuery.append(column.getName());
            createTableQuery.append(" ");
            createTableQuery.append(column.getSqlType());
            if (column.isUnique()) {
                createTableQuery.append(" UNIQUE");
            }
            if (column.getName().toLowerCase().equals("_id".toLowerCase())) {
                createTableQuery.append(" PRIMARY KEY AUTOINCREMENT ");
            }
            if (column.getDefaultValue() != null) {
                createTableQuery.append(" DEFAULT " + column.getDefaultValue());
            }
            createTableQuery.append(",");
        }
        createTableQuery.deleteCharAt(createTableQuery.length() - 1);
        createTableQuery.append(");");
        return createTableQuery.toString();
    }

    public String getCreateTriggerCommand() {
        StringBuilder createTriggerQuery = new StringBuilder("CREATE TRIGGER ");
        createTriggerQuery.append("update_time_");
        createTriggerQuery.append(getTableName());
        createTriggerQuery.append(" AFTER UPDATE ON ");
        createTriggerQuery.append(getTableName());
        createTriggerQuery.append(" BEGIN ");
        createTriggerQuery.append("UPDATE ");
        createTriggerQuery.append(getTableName());
        createTriggerQuery.append(" SET ");
        createTriggerQuery.append("_date_updated");
        createTriggerQuery.append(" = CURRENT_TIMESTAMP WHERE ");
        createTriggerQuery.append("_id");
        createTriggerQuery.append(" = new.");
        createTriggerQuery.append("_id");
        createTriggerQuery.append(";");
        createTriggerQuery.append(" END ");
        return createTriggerQuery.toString();
    }

    public PblDataModel addColumn(Column column) {
        this.mColumnMap.put(column, null);
        return this;
    }

    public Set<Column> getColumnSet() {
        return this.mColumnMap.keySet();
    }
}
